from typing import List

import pandas as pd
import geopandas as gpd

from .vector_dataset import VectorDataset


def stack(vector_datasets: List[VectorDataset]) -> VectorDataset:
    """Stack a list of vector datasets together into a new one.

    Parameters
    ----------
    vector_datasets : List[VectorDataset]
        A list of vector datasets to be stacked together,
        assuming all have the same set of columns, and the geometry columns are under the same CRS.

    Returns
    -------
    VectorDataset
        The resulting vector dataset.
    """
    new_gdf = pd.concat([vds._gdf for vds in vector_datasets])
    return VectorDataset(new_gdf)


def spatial_join(
    vds_left: VectorDataset,
    vds_right: VectorDataset,
    how: str = "inner",
    op: str = "intersects",
) -> VectorDataset:
    """Spatially join two vector datasets

    Parameters
    ----------
    vds_left : VectorDataset
        Vector dataset to be joined.
    vds_right : VectorDataset
        Another Vector dataset to be joined.
    how : str, optional
        How to do the join, could be one of "left", "right", "inner",
        by default "inner".
    op : str, optional
        Spatial join operator to be used, by default "intersects".
        You may check
        <https://geopandas.org/docs/reference/api/geopandas.GeoDataFrame.sjoin.html#geopandas.GeoDataFrame.sjoin>
        for supported methods.

    Returns
    -------
    VectorDataset
        The resulting joined vector dataset.
    """
    new_gdf = gpd.sjoin(
        vds_left._gdf, vds_right._gdf, how=how, op=op, lsuffix="left", rsuffix="right"
    )
    return VectorDataset(new_gdf)
